<template>
  <el-dialog
    v-model="showBind"
    width="320px"
    :align-center="true"
    :title="title"
    :close-on-click-modal="false"
    :destroy-on-close="true"
  >
    <div v-show="showOldPhone" class="old__phone">
      <form-list
        ref="refOldForm"
        :label="['phone', 'code']"
        :disbale-label="['phone']"
        :default-value="{ phone: user.userInfo.PhoneNumber }"
      />
      <div class="actions">
        <el-button
          type="primary"
          class="main-btn"
          size="large"
          @click="handleValidateOldPhone"
        >
          验证
        </el-button>
      </div>
    </div>
    <div v-show="showNewPhone" class="bind__phone">
      <form-list ref="refForm" :label="['phone', 'code']" />
      <div class="actions">
        <el-button
          type="primary"
          class="main-btn"
          size="large"
          @click="handleBindPhone"
        >
          确定
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { computed, ref } from "vue";
import formList, { type API } from "@/components/phone-form/form.vue";
import { bindAuth, checkSms } from "@/api/login";
import PsMessage from "@/components/message/index";
import { userAppStore } from "@/stores/user";
import { systemAppStore } from "@/stores/system";
import { invokeAPi } from "@/xz-fn";

export interface BindPhoneApi {
  open: (type: string) => void;
}

const title = computed(() => {
  if (showOldPhone.value) return "旧手机验证";
  return "新手机号码";
});
const user = userAppStore();
const system = systemAppStore();
const showBind = ref(false);
const refForm = ref<API>();
const refOldForm = ref<API>();

const showOldPhone = ref(false);
const showNewPhone = ref(false);

let oldForm: any = null;

const open = () => {
  showBind.value = true;
  if (user.userInfo.PhoneNumber) {
    showOldPhone.value = true;
    showNewPhone.value = false;
  } else {
    showOldPhone.value = false;
    showNewPhone.value = true;
  }
};

const handleBindPhone = async () => {
  const form = await refForm.value?.validate();
  if (!form) return;
  const params: any = {
    auth_type: "phone",
    phone: form.phone,
    auth_code: form.code,
    code_id: form.phone,
    bind_type: 0
  };
  if (oldForm) {
    params.old_phone = oldForm.phone;
    params.old_auth_code = oldForm.code;
    params.old_code_id = oldForm.phone;
    params.bind_type = 1;
  }

  const res = await bindAuth(params);
  if (!res.data.need_merge) {
    showBind.value = false;
    user.getUserInfo();
    PsMessage("绑定成功");
    invokeAPi('fq_NotifyWebMessageEx', '', {
      action: 'bindSuccess'
    })
  }else{
    PsMessage("当前绑定的手机号码已存在，请更换手机号码");
  }
};

const handleValidateOldPhone = async () => {
  const form = await refOldForm.value?.validate();
  if (!form) return;
  const params = {
    code: form.code,
    codeid: form.phone
  };

  const data:any = await checkSms(params);
  if (data?.data?.inEffect) {
    oldForm = form;
    showOldPhone.value = false;
    showNewPhone.value = true;
    return;
  }
  PsMessage(data?.msg || "请求错误，请稍后再试");
};

defineExpose({
  open
});
</script>

<style lang="scss" scoped>
.bind__phone {
  padding: 0 10px;
  color: var(--el-text-color-primary);
}
h2 {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  padding-bottom: 20px;
}
.main-btn {
  width: 100%;
  margin: 20px 0 10px 0;
}
</style>
